<template>
  <div class="pic"/>
</template>

<script>
import Qs from 'qs'
import wx from 'weixin-js-sdk'
export default {
  layout: 'default3',
  data() {
    return {
      ts: this.newStore()
    }
  },
  mounted() {
    const query = Qs.parse(window.location.search.substring(1))
    const code = query.code
    if (code && code != '') {
      this.doLogin(code)
      return
    }
    this.getWxCode()
  },

  methods: {
    doLogin(code) {
      const res = {
        code: code
      }
      this.spost(this.ts, '/Register/getWeixinCode', res).then(ts => {
        // console.log(ts.d.data)
        // alert(ts.d.data.identity)
        if (ts.d.data.identity == '1') {
          mui.alert('没有操作此功能权限！', '权限提示')
          return
        }
        localStorage.setItem('token', ts.d.data.token)
        this.spost(this.ts, '/about/getWxConfig', {
          url: location.href.split('#')[0]
        }).then(ts2 => {
          wx.config({
            debug: false,
            appId: ts2.d.data.appId,
            timestamp: ts2.d.data.timestamp,
            nonceStr: ts2.d.data.nonceStr,
            signature: ts2.d.data.signature,
            jsApiList: ['scanQRCode']
          })
          wx.ready(() => {
            wx.scanQRCode({
              needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
              scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
              success: res => {
                var result = res.resultStr // 当needResult 为 1 时，扫码返回的结果
                // alert(result)
                // console.log(result)

                this.$router.push({
                  name: 'ddpage3',
                  query: {
                    orderno: this.getQueryString(result, 'orderno'),
                    ordertoken: this.getQueryString(result, 'ordertoken'),
                    nowtime: this.getQueryString(result, 'nowtime')
                  }
                })
              }
            })
          })
        })
      })
    },
    getQueryString(url, name) {
      let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
      let r = url.match(reg)
      if (r != null) {
        return unescape(r[2])
      }
      return null
    },
    getWxCode() {
      const wxLoginUrl =
        'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf30a6608e950a663&redirect_uri=http://vipcard.eke88.com/sm&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'

      // 改成ajax试试
      window.location = wxLoginUrl
    }
  }
}
</script>

<style>
.pic {
  height: 100vh;
  background: url('../static/images/timg.gif');
  background-position: center;
}
</style>
